<?php
/*
 * Tạo Support Product widget
* */
class Products extends WP_Widget
{
	
	//Khởi tạo contructor của 1 lớp
	function Products()
	{
		parent::WP_Widget('Products_Widget',
				esc_attr(__('Căn hộ Bán/Cho thuê',THMENAME)),
				array('description' => esc_attr(__('This for setting value Products in category.',THEMENAME))));

	}

	function widget( $args, $instance ) // hien thi noi dung widget
	{
		global $currency;
		extract($args);
		$title = apply_filters( 'widget_title',
				empty($instance['title']) ? '' : $instance['title'],
				$instance, $this->id_base);

		$showposts = apply_filters( 'widget_text',
				$instance['showposts'], $instance );

		$selected = apply_filters( 'widget_text',
				$instance['selected'], $instance );
		
		$banchothue = apply_filters( 'widget_text',
				$instance['banchothue'], $instance );
		
		$todisplay = apply_filters( 'widget_text',
				$instance['todisplay'], $instance );

		?>
		 <div class="module">
			<div>
				<div>
					<div>
					<h3 style="line-height: 45px;"><?php echo $title;?></h3>
					<div id="content">
					<div class="ja-sidenews-list clearfix">
					<?php			

					
					if($banchothue!='0')
					{
						$duan = new WP_Query(array('post_type' => 'post','orderby'=>'modified','order'=>'DESC','showposts' => $showposts,'tax_query' => array(array('taxonomy'=>'ban-cho-thue','field'=>'slug','terms'=>$banchothue))));						
					}
					else
					{	
						if($selected!=0) 	 
							$duan = new WP_Query(array('post_type' => 'post','orderby'=>'modified','order'=>'DESC','showposts' => $showposts,'tax_query' => array(array('taxonomy'=>'category','field'=>'slug','terms'=>$selected))));
						else
							$duan = new WP_Query(array('post_type' => 'post','orderby'=>'modified','order'=>'DESC','showposts' => $showposts));
					}	
					
					if($duan->have_posts()):
					while ($duan->have_posts()):
						$duan->the_post();
						
						if($todisplay==0)
							$todisplay=1;						
						include TEMPLATEPATH.'/includes/widgets/latest-layout'.$todisplay.'.php';
					?>
					
			  		<?php endwhile;?>
			  		<?php 			  		
			  		endif;
			  		
			  		$duan = '';
			  		?>
  				
  					</div>                   
  					</div>
					</div>
				</div>
			</div>
		</div>					
		<?php		
	}

	function update( $new_instance, $old_instance ) 
	{
		$instance = $old_instance;
				
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['showposts'] = strip_tags($new_instance['showposts']);		
		$instance['selected'] = strip_tags($new_instance['selected']);
		$instance['banchothue'] = strip_tags($new_instance['banchothue']);
		$instance['todisplay'] = strip_tags($new_instance['todisplay']);
			
		return $instance;
	}

	function form( $instance ) 
	{
		$instance = wp_parse_args( (array) $instance, 
			array( 'title' => '','todisplay' => '', 'showposts' => '','selected'=>'' ) );
		$title = strip_tags($instance['title']);
		$showposts = strip_tags($instance['showposts']);
		$todisplay = strip_tags($instance['todisplay']);
		$selected = strip_tags($instance['selected']);
		$banchothue = strip_tags($instance['banchothue']);
?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">
				<?php echo  __('Tiêu đề:'); ?> </label>
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" 
				name="<?php echo $this->get_field_name('title'); ?>" type="text" 
				value="<?php if($title) echo  esc_attr($title);?>" />
		</p>
	
		<p>
			<label for="<?php echo $this->get_field_id('showposts'); ?>">
				<?php _e('Showposts:'); ?> </label>
			<input class="widefat" id="<?php echo $this->get_field_id('showposts'); ?>" 
				name="<?php echo $this->get_field_name('showposts'); ?>" type="text" 
				value="<?php if($showposts) echo  esc_attr($showposts); else echo '5';?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('category'); ?>">
				<?php echo __('Danh mục dự án:',THEMENAME); ?> </label>						
				
				<?php 
				$args = array(
						'show_option_all'    => 'Chọn danh mục',
						'show_option_none'   => '',
						'orderby'            => 'ID',
						'order'              => 'ASC',
						'show_count'         => 0,
						'hide_empty'         => 1,
						'child_of'           => 0,
						'exclude'            => 1,
						'echo'               => 1,
						'selected'           => $selected,
						'hierarchical'       => 0,
						'name'               => 'selected',
						'id'                 => 'selected',
						'class'              => 'postform',
						'depth'              => 0,
						'tab_index'          => 0,
						'taxonomy'           => 'category',
						'hide_if_empty'      => false,
						'walker'             => ''
				);
							
				$danhm = get_categories($args);
				if($danhm)
				{	
				?>
				<select class="widefat" id="<?php echo $this->get_field_id('selected'); ?>" 
					name="<?php echo $this->get_field_name('selected'); ?>">
					<option value="0"><?php echo __('Chọn danh mục',THEMENAME);?></option>
					<?php 
					foreach ($danhm as $val)
					{
						if($val->slug == $banchothue)
							echo '<option selected="selected" value="'.$val->slug.'">'.$val->name.'</option>';
						else 
							echo '<option value="'.$val->slug.'">'.$val->name.'</option>';
					}	
					?>
					
				</select>
				
				<?php 
				}
				?>
				
				<!-- 
				<select class="widefat" id="<?php echo $this->get_field_id('selected'); ?>" 
					name="<?php echo $this->get_field_name('selected'); ?>">
					<option value=""><?php echo __('Mặc định',THEMENAME);?></option>
					<option <?php if($selected=='can-ban') echo 'selected="selected"';?> value="<?php echo 'can-ban';?>"><?php echo __('Cần bán',THEMENAME);?></option>
					<option <?php if($selected=='cho-thue') echo 'selected="selected"';?> value="<?php echo 'cho-thue';?>"><?php echo __('Cho thuê',THEMENAME);?></option>
					
				</select>
				 -->
				
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('banchothue'); ?>">
				<?php echo __('Bán/cho thuê:',THEMENAME); ?> </label>						
				
				<?php 
				$args = array(
						'show_option_all'    => 'Chọn danh mục',
						'show_option_none'   => '',
						'orderby'            => 'ID',
						'order'              => 'ASC',
						'show_count'         => 0,
						'hide_empty'         => 1,
						'child_of'           => 0,
						'exclude'            => 1,
						'echo'               => 1,
						'selected'           => $banchothue,
						'hierarchical'       => 0,
						'name'               => 'banchothue',
						'id'                 => 'banchothue',
						'class'              => 'postform',
						'depth'              => 0,
						'tab_index'          => 0,
						'taxonomy'           => 'ban-cho-thue',
						'hide_if_empty'      => false,
						'walker'             => ''
				);
				
				$bct = get_categories($args);
				if($bct)
				{	
				?>
				<select class="widefat" id="<?php echo $this->get_field_id('banchothue'); ?>" 
					name="<?php echo $this->get_field_name('banchothue'); ?>">
					<option value="0"><?php echo __('Chọn bán/cho thuê',THEMENAME);?></option>
					<?php 
					foreach ($bct as $val)
					{
						if($val->slug == $banchothue)
							echo '<option selected="selected" value="'.$val->slug.'">'.$val->name.'</option>';
						else 
							echo '<option value="'.$val->slug.'">'.$val->name.'</option>';
					}	
					?>
					
				</select>
				
				<?php 
				}
				?>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id('todisplay'); ?>">
				<?php echo __('Chọn hiển thị:',THEMENAME); ?> </label>						
				
				<select class="widefat" id="<?php echo $this->get_field_id('todisplay'); ?>" 
					name="<?php echo $this->get_field_name('todisplay'); ?>">
					<option value="0"><?php echo __('Mặc định',THEMENAME);?></option>
					<option <?php if($todisplay=='1') echo 'selected="selected"';?> value="1"><?php echo __('Kiểu 1',THEMENAME);?></option>
					<option <?php if($todisplay=='2') echo 'selected="selected"';?> value="2"><?php echo __('Kiểu 2 không thumb',THEMENAME);?></option>
					<option <?php if($todisplay=='3') echo 'selected="selected"';?> value="3"><?php echo __('Kiểu 3 Tiêu đề',THEMENAME);?></option>
					
				</select>
				
		</p>
		
<?php
	}
}
// End Support Product widget

register_widget('Products');

//===========================================================================================================================


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++